/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.merb.client.ui;

import co.com.merb.client.bosquejo.Bosquejos;
import co.com.merb.client.service.LoginService;
import co.com.merb.client.service.LoginServiceAsync;
import co.com.merb.client.ui.menu.InicioPane;
import co.com.merb.shared.i18n.MerbConstants;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.TextBox;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;

/**
 *
 * @author alexkbra
 */
public class Login extends Window {

    private final MerbConstants constants;
    private final LoginServiceAsync serviceLogin;
    private final SectionStack sectionStack;
    private final SectionStackSection formation;
    private final TextItem txtUsuario;
    private final TextItem txtPass;
    private final TextBox txtEjemplo;
    private final ButtonItem btnIngresar;
    private final DynamicForm loginForm;
    private final Bosquejos bosquejos;

    public Login() {
        this.constants = GWT.create(MerbConstants.class);
        this.serviceLogin = GWT.create(LoginService.class);
        this.txtEjemplo = new TextBox();
        this.sectionStack = new SectionStack();
        this.formation = new SectionStackSection();
        this.txtUsuario = new TextItem();
        this.txtPass = new TextItem();
        this.btnIngresar = new ButtonItem(constants.Ingresar());
        this.loginForm = new DynamicForm();
        this.bosquejos = new Bosquejos();
        this.setComponents();
    }

    private void setComponents() {
        setHeight("30%");
        setWidth("35%");
        
        HLayout hLayout = new HLayout();
        
        formation.setCanCollapse(false);
        formation.setExpanded(true);

        txtUsuario.setRequired(true);
        txtUsuario.setTooltip("Usuario");
        txtUsuario.setWidth(bosquejos.Anchotexto());
        txtUsuario.setHeight(bosquejos.Altotexto());
        txtUsuario.setRequiredMessage(constants.MensajeRequerido());
        txtUsuario.setTitle(constants.Usuario());

        txtPass.setRequired(true);
        txtPass.setTooltip("Clave del usuario");
        txtPass.setWidth(bosquejos.Anchotexto());
        txtPass.setHeight(bosquejos.Altotexto());
        txtPass.setRequiredMessage(constants.MensajeRequerido());
        txtPass.setTitle(constants.Pass());
        
        KeyPressHandler keyPressHandler = new KeyPressHandler() {
            @Override
            public void onKeyPress(KeyPressEvent event) {
                if (event.getKeyName().equals("Enter")) {
                    entrar();
                }
            }
        };
        
        txtUsuario.addKeyPressHandler(keyPressHandler);
        txtPass.addKeyPressHandler(keyPressHandler);
        
        btnIngresar.setColSpan(2);
        btnIngresar.setAlign(Alignment.CENTER);
        btnIngresar.setWidth(bosquejos.AnchoBoton());
        btnIngresar.setHeight(bosquejos.AltoBoton());
        btnIngresar.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                entrar();
            }
        });
        
        loginForm.setNumCols(2);
        loginForm.setAlign(Alignment.CENTER);
        loginForm.setFields(txtUsuario, txtPass, btnIngresar);
        formation.addItem(loginForm);
        sectionStack.setSections(formation);

        setShowCloseButton(false);
        setShowMinimizeButton(false);
        setIsModal(true);
        setShowModalMask(true);
        setCanDragReposition(false);
        centerInPage();
        setTitle(constants.TituloProyecto());
        addItem(sectionStack);
    }

    public void entrar() {
        if (loginForm.validate()) {
            final String usuario = txtUsuario.getValueAsString(),
                    pass = txtPass.getValueAsString();
            serviceLogin.ingresar(usuario, pass, new AsyncCallback<Boolean>() {
                @Override
                public void onFailure(Throwable caught) {
                    SC.say(constants.MensajeError(), caught.getMessage());

                }

                @Override
                public void onSuccess(Boolean result) {
                    if (result) {
                        InicioPane inicioPane = new InicioPane();
                        inicioPane.show();
                        destroy();
                    }
                }
            });
        }
    }
}
